#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int a[N],b[N];
int sum=0;


int main(){
	freopen("input.txt","r",stdin);
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int max=0,min=9999999;
	for(int i=0;i<n;i++){
		if(a[i]>max){
			max=a[i];
		}else{
			b[i]=1;sum++;
		}
	}
	for(int i=n-1;i>=0;i--){
		if(a[i]<min){
			min=a[i];
		}else{
			if(b[i]==0){
				b[i]=1;sum++;
			}
		}
	}
	cout<<n-sum<<endl;int flag=1;
	for(int i=0;i<n;i++){
		if(b[i]==0){
			if(flag){
				cout<<a[i];flag=0;
			}else{
				cout<<" "<<a[i];
			}
		}

	}
	return 0;


}